#pragma once

#include "DDRequest.h"

#include <syscall.h>

class AsyncMsg final
{
    // 单条记录多个字段
    using RecordType = std::unordered_map<int, std::string>;
    // 多记录集合
    using RecordsType = std::vector<RecordType>;

public:
    AsyncMsg(std::string account, std::string password, HANDLE_CONN conn);
    ~AsyncMsg();

    void SetCallback(int type = 1);

    bool AsyncLoginEx();

    void AsyncQueryStockAccount();

    void AsyncQueryRiskInfo();

private:
    static bool OnSessionReply(HANDLE_CONN hConn, HANDLE_SESSION hSess, int nRecv);
    static bool OnSessionReply2(HANDLE_CONN hConn, HANDLE_SESSION hSess, int nRecv);

    void ParseItemsToCache(HANDLE_CONN hConn, HANDLE_SESSION hSess);

    void DoPrintLog();

    void ConfirmLogin(HANDLE_SESSION hSess);

private:
    std::string m_AccountId;
    std::string m_Password;

    HANDLE_CONN m_Conn{-1};

    FixInfo m_FixInfo;

    RecordsType m_CacheRecords;
    char *m_CachedBuf = nullptr;
    RecordType m_CacheRecord;

public:
    std::atomic_bool m_IsLogin{false};

    std::condition_variable m_Cv;
    std::mutex m_Mtx;
};

extern AsyncMsg *g_AsyncMsg;
extern AsyncMsg *g_AsyncMsg2;
